package com.intellagric.service.iftmonitor.Impl.ift_ctrl_equipment;

import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.mapper.IftCtrlEquipmentTypeMapper;
import com.intellagric.pojo.IftCtrlEquipmentType;
import com.intellagric.pojo.IftCtrlEquipmentTypeExample;
import com.intellagric.service.iftmonitor.ift_ctrl_equipment.CtrlEquipmentTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/10/21
 * @DESC： 控制设备类型服务实现
 */
@Service
public class CtrlEquipmentTypeServiceImpl implements CtrlEquipmentTypeService {

    @Autowired
    private IftCtrlEquipmentTypeMapper ctrlEquipmentTypeMapper;

    /**
     * 添加控制设备类型
     * @param ctrlEquipmentType
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addCtrlEquipmentType(IftCtrlEquipmentType ctrlEquipmentType) {
        if (ctrlEquipmentTypeMapper.insertSelective(ctrlEquipmentType) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除控制设备类型
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteCtrlEquipmentType(String id) {
        if (ctrlEquipmentTypeMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改控制设备类型信息
     * @param ctrlEquipmentType
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editCtrlEquipmentType(IftCtrlEquipmentType ctrlEquipmentType) {
        if (ctrlEquipmentTypeMapper.updateByPrimaryKeySelective(ctrlEquipmentType) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 查询控制设备类型信息
     * @param id
     * @return IftCtrlEquipmentType
     */
    @Override
    public IftCtrlEquipmentType getCtrlEquipmentTypeById(String id) {
        return ctrlEquipmentTypeMapper.selectByPrimaryKey(id);
    }

    /**
     * 异步查询通知设备分类信息
     * @param parentId
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getCtrlEquipmentTypePid(String parentId) {
        IftCtrlEquipmentTypeExample ctrlEquipmentTypeExample = new IftCtrlEquipmentTypeExample();
        ctrlEquipmentTypeExample.setOrderByClause("sort");//升序排列
        ctrlEquipmentTypeExample.createCriteria().andParentIdEqualTo(parentId);
        List<IftCtrlEquipmentType> ctrlEquipmentTypeList = ctrlEquipmentTypeMapper.selectByExample(ctrlEquipmentTypeExample);
        LayuiDataGridResult result = new LayuiDataGridResult();
        result.setData(ctrlEquipmentTypeList);
        return result;
    }

    /**
     * 查询通知设备分类信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getCtrlEquipmentTypeList() {
        IftCtrlEquipmentTypeExample ctrlEquipmentTypeExample = new IftCtrlEquipmentTypeExample();
        ctrlEquipmentTypeExample.setOrderByClause("sort");//升序排列
        ctrlEquipmentTypeExample.createCriteria().andIdIsNotNull();
        List<IftCtrlEquipmentType> ctrlEquipmentTypeList = ctrlEquipmentTypeMapper.selectByExample(ctrlEquipmentTypeExample);
        return LayuiDataGridResult.success().add(ctrlEquipmentTypeList,ctrlEquipmentTypeList.size());
    }

    /**
     * 查询通知设备分类信息列表,并生成树形数据
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getCtrlEquipmentTypeTree() {
        //获取所有分类列表
        List<IftCtrlEquipmentType> ctrlEquipmentTypeList = this.getCtrlEquipmentTypeList().getData();

        //获取顶级分类列表
        List<IftCtrlEquipmentType> rootCtrlEquipmentTypeList= this.getCtrlEquipmentTypePid("0").getData();

        for (IftCtrlEquipmentType ctrlEquipmentType : rootCtrlEquipmentTypeList) {
            ctrlEquipmentType.setChildren(getChild(ctrlEquipmentType.getId(),ctrlEquipmentTypeList));
        }
        LayuiDataGridResult result = new LayuiDataGridResult();
        result.setData(rootCtrlEquipmentTypeList);
        return result;
    }

    private List<IftCtrlEquipmentType> getChild(String id, List<IftCtrlEquipmentType> ctrlEquipmentTypeList) {
        // 子菜单
        List<IftCtrlEquipmentType> childList = new ArrayList<>();

        // 遍历所有节点，将父菜单id与传过来的id比较
        for (IftCtrlEquipmentType ctrlEquipmentType : ctrlEquipmentTypeList) {
            if (ctrlEquipmentType.getParentId() != null) {   //所有非以及菜单的都要加入chidList中（递归）
                if (ctrlEquipmentType.getParentId().equals(id)) {
                    childList.add(ctrlEquipmentType);
                }
            }
        }

        // 把子菜单的子菜单再循环一遍
        for (IftCtrlEquipmentType ctrlEquipmentType : childList) {// 没有url子菜单还有子菜单（暂不考虑url的问题）
            // 递归
            ctrlEquipmentType.setChildren(getChild(ctrlEquipmentType.getId(), ctrlEquipmentTypeList));
        }
        // 递归退出条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }
}
